Skip to content

fix: Center period labels over their own period when using ticklabelindex - #7934

Open
my-tien wants to merge 6 commits into
plotly:mainfrom
my-tien:7876-ticklabelindex-positioning
Open

my-tien wants to merge 6 commits into
plotly:mainfrom
my-tien:7876-ticklabelindex-positioning

Conversation

@my-tien

@my-tien my-tien commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

Fixes #7876 by rewriting the way period label positioning works when using ticklabelindex.

Previously, the next labeled tick was used as the period end for the previous tick. This was problematic when using ticklabelindex, because sometimes the next labeled period is not necessarily adjacent to the last labeled period.
Now positionPeriodTicks can accept an array of periodEndTicks instead.

@my-tien
my-tien marked this pull request as draft August 5, 2026 13:01
@my-tien
my-tien marked this pull request as ready for review August 5, 2026 13:32
@robertclaus
robertclaus requested a review from camdecoster August 6, 2026 19:33

@camdecoster camdecoster left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Most of this works, but there are a few changes that need to be made. I'm happy to talk through how to do some of this.

}
return minor;
})
.toSorted((a, b) => a.value - b.value);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It turns out I was wrong before about esbuild. It won't transpile this method, so you'll have to use sort instead.

Suggested change
.toSorted((a, b) => a.value - b.value);
.slice()
.sort((a, b) => a.value - b.value);

Comment on lines +1013 to +1015
// for period label positioning when using `ticklabelindex`:
// for each tick in `allTicklabelVals` holds the neighboring period end tick
var periodEndTicks;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of saving an array of end ticks for each tick, what if we save the end tick to each tick? Then you could check for an end tick on each tick and use a different code path. You'd also be able to get rid of the new arg in positionPeriodTicks.

// This minor tick will be labeled instead of the major tick.
if (isPeriod) periodEndTicks = []; // for each minor tick at the start of a labeled period this will hold the neighboring period end tick.

const labelTickValsAscending = minorTickVals

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What should happen if there's a major tick that doesn't coincide with a minor tick? Should that also be included?

var isReversed = ax.range[0] > ax.range[1];
var ticklabelIndex = (!ax.ticklabelindex || Lib.isArrayOrTypedArray(ax.ticklabelindex)) ?
ax.ticklabelindex : [ax.ticklabelindex];
ax._useTicklabelIndex = ticklabelIndex != null && ticklabelIndex !== 0;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is only used inside calcTicks, so it can just be a local variable.

Suggested change
ax._useTicklabelIndex = ticklabelIndex != null && ticklabelIndex !== 0;
let useTicklabelIndex = ticklabelIndex != null && ticklabelIndex !== 0;

Comment on lines +1247 to +1248
majorTick.skipLabel = majorTick.skipLabel !== false;
});

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

majorTick.skipLabel will always be true given the if check above. Let's move this out of the inner loop and assign the value directly.

Suggested change
majorTick.skipLabel = majorTick.skipLabel !== false;
});
});
// Skip the major tick label since the label moved to a minor tick
majorTick.skipLabel = true;

if (labelIndex < largerTicks.length - 1) {
allTicklabelVals.push(largerTicks[labelIndex]);
if (isPeriod) periodEndTicks.push(largerTicks[labelIndex + 1]);
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand why you're using length -1, but that means that you're going to lose the last label. You could run through the entire array and check that largerTicks[labelIndex + 1] is defined before pushing it. This logic would also need to change if you start adding the end tick directly to the tick itself.

@camdecoster camdecoster changed the title Fix #7876: Ticklabelindex positioning fix: Center period labels over their own period when using ticklabelindex Sep 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG]: period labels can be positioned incorrectly when using ticklabelindex with only one minor tick

2 participants